﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FORUs
{
    public class UltrastarPlaylist
    {
        #region Local Variables
        private string _sName;
        private List<UltrastarSong> _lsSongs;
        private string _sFilename;
        #endregion

        #region Properties
        public string Name
        {
            get { return _sName; }
            set { _sName = value; }
        }

        public string Filename
        {
            get { return _sFilename; }
        }

        public int SongsCount
        {
            get { return _lsSongs.Count; }
        }

        public bool IsEmpty
        {
            get { return _sFilename.Equals(string.Empty); }
        }
        #endregion

        #region Constructors
        private UltrastarPlaylist()
        {
            _lsSongs = new List<UltrastarSong>();
        }
        #endregion

        #region Songs Management
        public bool AddSong(UltrastarSong uSong)
        {
            // Check if the song is already added
            if (_lsSongs.Contains(uSong))
                return false;

            // Add the song
            _lsSongs.Add(uSong);
            return true;
        }

        public bool RemoveSongAt(int index)
        {
            if ((index < 0) || (index >= _lsSongs.Count))
                return false;

            _lsSongs.RemoveAt(index);
            return true;
        }

        public bool RemoveSong(UltrastarSong uSong)
        {
            // Check if the song exists in the playlist
            if (!_lsSongs.Contains(uSong))
                return false;

            // Remove the song
            return _lsSongs.Remove(uSong);
        }

        public bool RemoveSong(string songFilename)
        {
            for(int i=0; i<_lsSongs.Count; i++)
            {
                if (_lsSongs[i].Filename.Equals(songFilename))
                {
                    _lsSongs.RemoveAt(i);
                    return true;
                }
            }

            return false;
        }
        #endregion

        #region Loading and Saving
        public static UltrastarPlaylist FromFile(string filename)
        {
            UltrastarPlaylist uPlaylist = new UltrastarPlaylist();
            StreamReader reader = null;
            string sLine;

            // Checks if the file exists
            if (!File.Exists(filename))
                return Empty;

            try
            {
                uPlaylist._sFilename = filename;
                reader = new StreamReader(filename, Encoding.Default, true);

                // Find the name of the playlist
                do
                {
                    sLine = reader.ReadLine();
                } while (!sLine.StartsWith("#Name:"));
                uPlaylist._sName = sLine.Substring(7);

                // Get the songs
                do
                {
                    if (!sLine.StartsWith("#"))
                    {
                        // The filename is not mentioned - we use VirtualUltrastarSong object instead
                        string artist = sLine.Substring(0, sLine.IndexOf(" : "));
                        string title = sLine.Substring(sLine.IndexOf(" : ") + 3);
                        UltrastarSong uSong = new VirtualUltrastarSong(artist, title);
                        uPlaylist._lsSongs.Add(uSong);
                    }

                    sLine = reader.ReadLine();
                } while (sLine != null);
            }
            catch (Exception)
            {
                uPlaylist = Empty;
            }
            finally
            {
                reader.Close();
            }

            return uPlaylist;
        }

        public static UltrastarPlaylist CreateTo(string filename, string name)
        {
            UltrastarPlaylist uPlaylist = new UltrastarPlaylist();
            if (File.Exists(filename))
                return Empty;
            
            uPlaylist._sFilename = filename;
            uPlaylist._sName = name;
            return uPlaylist;
        }

        public bool Save()
        {
            StreamWriter writer = null;
            bool bResult = true;

            try
            {
                writer = new StreamWriter(_sFilename, false, Encoding.Default);
                writer.WriteLine("######################################");
                writer.WriteLine("#Ultrastar Deluxe Playlist Format v1.0");
                writer.WriteLine("#Playlist \"{0}\" with {1} Songs.", _sName, SongsCount);
                writer.WriteLine("######################################");
                writer.WriteLine("#Name: {0}", _sName);
                writer.WriteLine("#Songs:");
                foreach (UltrastarSong uSong in _lsSongs)
                {
                    writer.WriteLine("{0} : {1}", uSong.Artist, uSong.Title);
                }
            }
            catch (Exception)
            {
                bResult = false;
            }
            finally
            {
                writer.Close();
            }

            return bResult;
        }
        #endregion

        #region Static Functions
        public static UltrastarPlaylist Empty
        {
            get
            {
                UltrastarPlaylist upEmpty = new UltrastarPlaylist();
                upEmpty._sFilename = string.Empty;
                return upEmpty;
            }
        }
        #endregion

        #region Windows Forms interaction
        public void LoadIntoListBox(System.Windows.Forms.ListBox listBox)
        {
            listBox.Items.Clear();
            listBox.Items.AddRange(_lsSongs.ToArray());
        }
        #endregion

        #region DisplayFunctions
        public override string ToString()
        {
            return _sName;
        }
        #endregion
    }
}
